//
//  M_HomeSouSuoViewController.m
//  Model
//
//  Created by apple on 2018/7/11.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "M_HomeSouSuoViewController.h"
#import "M_HomeSouSuoTableViewCell.h"
#import "M_PhotoMineDetailViewController.h"

#import "MJRefresh.h"
#import "MJRefreshHeader.h"
#import "MJRefreshFooter.h"
#import "M_PhotoViewController.h"
#import "M_HomePageDetailViewController.h"
@interface M_HomeSouSuoViewController ()<UITableViewDelegate, UITableViewDataSource>

{
    NSString *typeSouSuo;
    
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableArray *biaoQianArr;//标签
@property (nonatomic, strong) NSMutableArray *HotModelnameArr;//热门模特
@property (nonatomic, strong) NSMutableArray *HotModelnameArr2;//热门模特
@property (nonatomic, strong) NSMutableArray *HottypeArr;//热门标签
@property (nonatomic, strong) NSMutableArray *HottypeArr2;//热门标签

@property (nonatomic, strong) UITextField *shuRuTF;
@property(nonatomic,strong) UIScrollView *sxssScrollView;
@property (nonatomic, assign) NSInteger page;


@end

@implementation M_HomeSouSuoViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    __weak typeof(self) weakself = self;
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [weakself creationDataJZ];
    }];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakself creationData];
        
        [self.tableView.mj_header beginRefreshing];
    }];


    
  
}
- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return  _dataArr;
}
- (NSMutableArray *)biaoQianArr{
    if (!_biaoQianArr) {
        _biaoQianArr = [NSMutableArray array];
    }
    return _biaoQianArr;
    
}
- (NSMutableArray *)HottypeArr{
    if (!_HottypeArr) {
        _HottypeArr = [NSMutableArray array];
    }
    return _HottypeArr;
    
}
- (NSMutableArray *)HottypeArr2{

    if (!_HottypeArr2) {
        _HottypeArr2 = [NSMutableArray array];
        
    }
    return _HottypeArr2;
}
- (NSMutableArray *)HotModelnameArr{
    if (!_HotModelnameArr) {
        _HotModelnameArr = [NSMutableArray array];
    }
    return _HotModelnameArr;
}
- (NSMutableArray *)HotModelnameArr2{
    if (!_HotModelnameArr2) {
        _HotModelnameArr2 = [NSMutableArray array];
    }
    return _HotModelnameArr2;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creationView];
    [self biaoQianData];
    typeSouSuo = @"1";
    _page = 1;
//    [self creationTable];
   
//    [self regeditCell];
    
    // Do any additional setup after loading the view.
}
- (void)creationView{
    UIImageView *rootImgView = [[UIImageView alloc] init];
    [self.view addSubview:rootImgView];
    rootImgView.userInteractionEnabled  = YES;
    rootImgView.image = [UIImage imageNamed:@"bg_top"];
    [rootImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(self.view).offset(0);
        make.height.offset(64+(KIsiPhoneX?24:0));
        
    }];
    
    UIView *headView = [[UIView alloc] init];
    [rootImgView addSubview:headView];
    [headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(40);
        make.right.equalTo(self.view).offset(-40);
        make.bottom.equalTo(rootImgView).offset(-10);
        make.height.offset(30);
        
    }];
    headView.layer.cornerRadius = 15;
    headView.layer.borderWidth = 1;
    headView.layer.borderColor = [UIColor whiteColor].CGColor;
    
    UIButton *backButton = [[UIButton alloc] init];
    [rootImgView addSubview:backButton];
    
    [backButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headView);
        make.left.equalTo(self.view).with.offset(10);
        make.width.offset(screenScale(20));
        make.height.offset(screenScale(20));
    }];
    [backButton addTarget:self action:@selector(leftNavItemClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *souSuoBtn = [[UIButton alloc] init];
    [rootImgView addSubview:souSuoBtn];
    
    [souSuoBtn setBackgroundImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    [souSuoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headView);
        make.right.equalTo(self.view).with.offset(-10);
        make.width.offset(screenScale(20));
        make.height.offset(screenScale(20));
    }];
    [souSuoBtn addTarget:self action:@selector(souSuoClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.shuRuTF = [[UITextField alloc] init];
    [headView addSubview:self.shuRuTF];
    [self.shuRuTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headView).with.offset(12);
        make.right.equalTo(headView).with.offset(-12);
        make.centerY.equalTo(headView);
        
        
    }];
    self.shuRuTF.placeholder = @"请输入搜索内容";
    self.shuRuTF.font = [UIFont systemFontOfSize:14];
    self.shuRuTF.textColor = [UIColor whiteColor];
    [self.shuRuTF setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    [self.shuRuTF setValue:[UIFont boldSystemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];

    
 
    
    
}
- (void)creationContentView{
    
    self.sxssScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, (KIsiPhoneX?94:74), DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-(KIsiPhoneX?94:74))];
    self.sxssScrollView.contentSize=CGSizeMake(DEF_SCREEN_WIDTH, 80+40+35*self.HotModelnameArr2.count/4+self.HotModelnameArr2.count*2+50 + 35*self.HottypeArr2.count/4+self.HottypeArr2.count*2+50);
    
    self.sxssScrollView.backgroundColor=[UIColor whiteColor];
    self.sxssScrollView.showsVerticalScrollIndicator=NO;
    self.sxssScrollView.pagingEnabled = NO;
    
    
    [self.view addSubview:self.sxssScrollView];
    
    UILabel *moTeLab = [[UILabel alloc] init];
    moTeLab.textColor = getColor(@"ff40c6");
    [self.sxssScrollView addSubview:moTeLab];
    [moTeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sxssScrollView).with.offset(38);
        make.centerX.equalTo(self.view);
 
    }];
    moTeLab.font = [UIFont systemFontOfSize:17];
    moTeLab.text = @"热门模特";
    
    UIImageView *moTeImgView = [[UIImageView alloc] init];
    [self.sxssScrollView addSubview:moTeImgView];
    UIImage *img1 = [UIImage imageNamed:@"line_1"];
    [moTeImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sxssScrollView).with.offset(40);
        make.centerX.equalTo(self.view);
        make.width.offset(img1.size.width);
        make.height.offset(img1.size.height);
        
    }];
    moTeImgView.image = [UIImage imageNamed:@"line_1"];
   
    for (int i = 0; i < self.HotModelnameArr.count; i++) {
        self.HotModelnameArr2 = [self.HotModelnameArr[i] objectForKey:@"Hotmodelname"];
        
    }
    
    for (int i = 0 ; i < self.HotModelnameArr2.count; i++) {
        float Start_X = 12.0;// 第一个按钮的X坐标
        float Start_Y = 80.0;// 第一个按钮的Y坐标
        float Width_Space = 12;// 2个按钮之间的横间距
        float Height_Space = 12;// 竖间距
        float Button_Height = 35;// 高
        float Button_Width = (DEF_SCREEN_WIDTH - 60) / 4;// 宽
        NSInteger index2 = i % 4;
        NSInteger page = i / 4;
        
        // 圆角按钮
        UIButton *aBt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        
        aBt.frame = CGRectMake(index2 * (Button_Width + Width_Space) + Start_X, page  * (Button_Height + Height_Space)+Start_Y, Button_Width, Button_Height);
        aBt.backgroundColor = getColor(@"ececec");
        [aBt setTitle:[self.HotModelnameArr2[i] objectForKey:@"name"] forState:UIControlStateNormal];
        aBt.tag = [[self.HotModelnameArr2[i] objectForKey:@"modelId"] integerValue];
        [aBt addTarget:self action:@selector(moteClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [aBt setTitleColor:getColor(@"727272") forState:UIControlStateNormal];
        aBt.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.sxssScrollView addSubview:aBt];
        aBt.layer.cornerRadius = 5;
        aBt.layer.masksToBounds = YES;
        
    }
    
    UILabel *biaoQianLab = [[UILabel alloc] init];
    biaoQianLab.textColor = getColor(@"ff40c6");
    [self.sxssScrollView addSubview:biaoQianLab];
    [biaoQianLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sxssScrollView).with.offset(80+40+35*self.HotModelnameArr2.count/4+self.HotModelnameArr2.count*2);
        make.centerX.equalTo(self.view);
        
    }];
    biaoQianLab.font = [UIFont systemFontOfSize:17];
    biaoQianLab.text = @"热门标签";
    
    UIImageView *biaoQianImgView = [[UIImageView alloc] init];
    [self.sxssScrollView addSubview:biaoQianImgView];
    UIImage *img2 = [UIImage imageNamed:@"line_1"];
    [biaoQianImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sxssScrollView).with.offset(80+40+35*self.HotModelnameArr2.count/4+self.HotModelnameArr2.count*2);
        make.centerX.equalTo(self.view);
        make.width.offset(img2.size.width);
        make.height.offset(img2.size.height);
        
    }];
    biaoQianImgView.image = [UIImage imageNamed:@"line_1"];
    
    for (int i = 0; i < self.HottypeArr.count; i++) {
        self.HottypeArr2 = [self.HottypeArr[i] objectForKey:@"hotype"];
        
    }
    
    
    for (int i = 0 ; i < self.HottypeArr2.count; i++) {
        float Start_X = 12.0;// 第一个按钮的X坐标
        float Start_Y = 80+40+35*self.HotModelnameArr2.count/4+self.HotModelnameArr2.count*2+50;// 第一个按钮的Y坐标
        float Width_Space = 12;// 2个按钮之间的横间距
        float Height_Space = 12;// 竖间距
        float Button_Height = 35;// 高
        float Button_Width = (DEF_SCREEN_WIDTH - 60) / 4;// 宽
        NSInteger index2 = i % 4;
        NSInteger page = i / 4;
        
        // 圆角按钮
        UIButton *aBt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        
        aBt.frame = CGRectMake(index2 * (Button_Width + Width_Space) + Start_X, page  * (Button_Height + Height_Space)+Start_Y, Button_Width, Button_Height);
        aBt.backgroundColor = getColor(@"ececec");
        [aBt setTitle:[self.HottypeArr2[i] objectForKey:@"typename"] forState:UIControlStateNormal];
        aBt.tag = [[self.HottypeArr2[i] objectForKey:@"id"] integerValue];
        
        [aBt setTitleColor:getColor(@"727272") forState:UIControlStateNormal];
        aBt.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.sxssScrollView addSubview:aBt];
        aBt.layer.cornerRadius = 5;
        aBt.layer.masksToBounds = YES;
        [aBt addTarget:self action:@selector(biaoQianClick:) forControlEvents:UIControlEventTouchUpInside];
        NSString *tyID = [NSString stringWithFormat:@"%ld", [[self.HottypeArr2[i] objectForKey:@"id"] integerValue]];
        objc_setAssociatedObject(aBt, "firstObject", [self.HottypeArr2[i] objectForKey:@"typename"], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        objc_setAssociatedObject(aBt, "secondObject", tyID, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        [aBt addTarget:self action:@selector(biaoQianClick:) forControlEvents:UIControlEventTouchUpInside];
        
        
    }
    
    
}

#pragma mark -------------  跳9宫格
- (void)moteClick:(UIButton *)sender{
    
    M_PhotoMineDetailViewController *vc = [[M_PhotoMineDetailViewController alloc] init];
    
   
    
    
    vc.modelId = sender.tag;
    
    [self.navigationController pushViewController:vc animated:YES];
    
}
#pragma mark -------------  直接搜索

- (void)biaoQianClick:(UIButton *)sender{
    
    M_HomePageDetailViewController *vc = [[M_HomePageDetailViewController alloc] init];
    
    vc.nameTit = objc_getAssociatedObject(sender, "firstObject");
    vc.orderStatus = [objc_getAssociatedObject(sender, "secondObject") integerValue];
    
    
    [self.navigationController pushViewController:vc animated:YES];
    
//    NSMutableDictionary *dic =
    
    
    
}


- (UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.frame = CGRectMake(0, (KIsiPhoneX?94:74), DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-(KIsiPhoneX?94:74));
        
        _tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
        
        _tableView.backgroundColor = getColor(@"f7f7f7");
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        
    }
    return _tableView;
    
    
}
//- (void)creationTable{
//
//    self.tableView = [[UITableView alloc] init];
//    self.tableView.frame = CGRectMake(0, (KIsiPhoneX?94:74), DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-(KIsiPhoneX?94:74));
//
//    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
//
//    self.tableView.backgroundColor = getColor(@"f7f7f7");
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    self.tableView.delegate = self;
//    self.tableView.dataSource = self;
//    [self.view addSubview:self.tableView];
//
//}
- (void)regeditCell{
    
    [self.tableView registerNib:[UINib nibWithNibName:@"M_HomeSouSuoTableViewCell" bundle:nil] forCellReuseIdentifier:@"M_HomeSouSuoTableViewCell"];

    
    
}
#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 0;
}

#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
    
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
    
    
}
#pragma mark - ---------- 每个Cell的内容 ----------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    
    
    M_HomeSouSuoTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"M_HomeSouSuoTableViewCell" forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSMutableDictionary *dic = self.dataArr[indexPath.row];
    [cell.headImgView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"head"]] placeholderImage:[UIImage imageNamed:@""]];
    cell.name.text = [dic objectForKey:@"name"];
    cell.titleLab.text = [NSString stringWithFormat:@"被%ld人关注 | 共%ld私照",[[dic objectForKey:@"FollowCount"] integerValue],[[dic objectForKey:@"ImgCount"] integerValue]];
    
    
    
    return cell;
    
    
    
    
}

#pragma mark - ---------- 每个Cell的点击事件 ----------
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    M_PhotoMineDetailViewController *vc = [[M_PhotoMineDetailViewController alloc] init];
    
    
    vc.modelId = [[self.dataArr[indexPath.row] objectForKey:@"id"] integerValue];
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
#pragma mark --------  返回
- (void)leftNavItemClick{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}
#pragma mark ------------ 搜索
- (void)souSuoClick{
    
    typeSouSuo = @"2";
    [self creationData];

    [self.shuRuTF resignFirstResponder];
    [self.sxssScrollView removeFromSuperview];
//    [self creationTable];
    [self.view addSubview:self.tableView];
    [self regeditCell];

    
    
}
#pragma mark ---------  搜索结果-----------
- (void)creationData{
    
    [self.tableView.mj_header endRefreshing];
    //    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    //    hud.tag = 500;
    //    //提示文本
    //    hud.labelText = @"正在加载...";
    //    //是否有遮罩
    //    hud.dimBackground = YES;
    
    _page = 1;
    NSString *strPage = [NSString stringWithFormat:@"%ld", (long)_page];
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    [productParams setValue:strPage forKey:@"page"];
    [productParams setValue:self.shuRuTF.text forKey:@"name"];
//    [productParams setValue:@"10" forKey:@"pagesize"];
    [[MNetworkHandler sharedInstance]conURL:SelName networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        if ([[returnData objectForKey:@"Status"]intValue]==200) {
            
            self.dataArr = [returnData objectForKey:@"Data"];
            
            
            NSLog(@"DDDDDDDDDDDD%@", self.dataArr);
            
            [self.tableView reloadData];
            //
            //
            //            [hud removeFromSuperview];
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"Msg"] duration:5.0f];
            //            [hud removeFromSuperview];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        //        [hud removeFromSuperview];
    }];
    
}
- (void)creationDataJZ{
    
    _page++;
    
    NSString *strPage = [NSString stringWithFormat:@"%ld", (long)_page];
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    [productParams setValue:strPage forKey:@"page"];
    [productParams setValue:self.shuRuTF.text forKey:@"name"];
//    [productParams setValue:@"10" forKey:@"pagesize"];
    
    
    [[MNetworkHandler sharedInstance]conURL:SelName networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        if ([[returnData objectForKey:@"Status"]intValue]==200) {
            
            NSMutableArray *arr =[returnData objectForKey:@"Data"];
            
            if (arr.count == 0) {
                
                [self.tableView.mj_footer endRefreshing];
                
            }else {
                
                [self.dataArr addObjectsFromArray:arr];
                
                NSLog(@"JZJZJZJZJZJJZJZJZJZJZJJZJZ%@", self.dataArr);
                
                
            }
            //
            //
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
            //
        }
        //
        else {
            //
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"Msg"] duration:5.0f];
            [self.tableView.mj_footer endRefreshing];
            //
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
    }];
    
}
#pragma mark ------------------  标签
- (void)biaoQianData{
    
    //    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    
    [[MNetworkHandler sharedInstance]conURL:typeandmodel networkType:NetWorkPOST params:nil delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        
        if ([[returnData objectForKey:@"Status"]intValue]==200) {
            
            NSMutableArray *hotmodelname1 = returnData[@"Data"];
            
            for (int i = 0; i < hotmodelname1.count; i++) {
                self.HotModelnameArr = [hotmodelname1[i] objectForKey:@"HotModelname"];
                self.HottypeArr = [hotmodelname1[i] objectForKey:@"Hottype"];
                
            }

             [self creationContentView];
//            self.HotModelnameArr = returnData[@"Data"][@"HotModelname"][@"Hotmodelname"];
//            self.HottypeArr = returnData[@"Data"][@"Hottype"][@"hotype"];
            NSLog(@"%@", self.HotModelnameArr);
            NSLog(@"%@", self.HottypeArr);
            
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"Msg"] duration:5.0f];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
    }];
    
    
    
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    
    return UIStatusBarStyleLightContent;
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
